<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2018/12/21
 * Time: 10:04
 */

namespace app\api\service;


class  ConditionFun
{
    private $arr;
    private $execute_court = array();
    private $fraud_type_display_name = array();
    /**
     * 根据type组装成数组
     * @param $result
     * @return array
     */
    public function getConditionsData($result)
    {
        # 定义一个空数组用来存储每次循环处理后的数组 key(type) => value(处理好数组的值)
        $arr = [
            'cross_partner' => [],
            'grey_list' => [],
            'frequency_distinct' => [],
            'frequency_one_dim' => [],
            'black_list' => [],
            'discredit_count' => [],
        ];
        foreach ($result as $key => $condition) {
            $type = $condition['type'];
            if ($type == 'cross_partner') {
                # 命中了多平台借贷统计
                $data = $this->cross_partner($condition);
                $arr = $this->push($arr, $data, $type);
            }
            if ($type == 'grey_list') {
                # 命中了关注名单规则
                $data = $this->grey_list($condition);
                $arr = $this->push($arr, $data, $type);
            }
            if ($type == 'frequency_distinct') {
                # 命中了频度关联个数统计
                $data = $this->frequency_distinct($condition);
                $arr = $this->push($arr, $data, $type);
            }
//            if ($type == 'frequency_one_dim') {
//                # 命中了频度出现次数统计（单维度）
//                $data = $this->frequency_one_dim($condition);
//                $arr = $this->push($arr, $data, $type);
//            }
            if ($type == 'black_list') {
                # 命中了风险名单规则
                $this->black_list($condition);
            }
            if ($type == 'discredit_count') {
                # 命中了信贷逾期统计规则
                $data = $this->discredit_count($condition);
                $arr = $this->push($arr, $data, $type);
            }
        }
        # 对处理好的$cross_partner数组进行二次处理
        if (array_key_exists('cross_partner', $arr)) {
            $cross_partner = $this->second_cross_partner($arr['cross_partner']);
            $arr['cross_partner'] = $cross_partner;
        }
        $arr['execute_court'] = $this->execute_court;
        $arr['black_list'] = $this->fraud_type_display_name;
        return $arr;
    }
    /**
     * 组装一个新的{风险决策接口}数组
     * @param $result
     * @return array
     */
    public function getService($result)
    {
        $data = $this->service_hit_rules($result,true);
        //$data = (json_decode($result,true));
        return $data;
    }

    /**
     * @param $arr 用来存储需求的数组
     * @param $data 每个独立方法查询的结果
     * @param $type type类型
     * @return mixed
     */
    private function push($arr,$data,$type)
    {
//        if(array_key_exists($type,$arr) == false){
        if($arr[$type] == false){
            $arr[$type] = $data;
        }else{
            # 相当于数组的追加
            $arr_count = count($arr[$type]);
            $data_count = count($data);
            for($i=0;$i<$data_count;$i++){
                $arr[$type][$arr_count+$i] = $data[$i];
            }
        }
        return $arr;
    }

    /**
     * type为grey_list 处理数组的方法
     * @param $type
     * @param $condition
     * @return array
     */
    private function grey_list($condition)
    {
        # 定义一个空数组用来作为筛选数组的容器
        $arr = [];
        $hits = $condition['hits'];
        foreach ($hits as $key => $value){
            unset($value['value']);
            $value['evidence_time'] = substr($value['evidence_time'],0,-3);
            array_push($arr,$value);
        }
        return $arr;
    }

    /**
     * type为cross_partner 处理数组的方法
     * 处理好的数组还需要进行去重和统计次数 $this->second_cross_partner()
     * @param $condition
     * @return array
     */
    private function cross_partner($condition)
    {
        # 定义一个空数组用来作为筛选数组的容器
        $arr = [];
        $hits = $condition['hits'];
        # 因为 $hits 数组是由每个相同的二维数组组成，所有unset需要在foreach里面销毁
        foreach ($hits as $key => $value){
            unset($value['original_dim_type']);
            unset($value['match_dim_type']);
            array_push($arr,$value);
        }
        return $arr;
    }
    /**
     * type为frequency_distinct 处理数组的方法 （多维度命中）
     * @param $condition
     * @return array
     */
    private function frequency_distinct($condition)
    {
        # 定义一个空数组用来作为筛选数组的容器
        $arr = [];
        $data = $condition;
        if(array_key_exists('list',$data) && array_key_exists('sub_dim_type',$data)){
            $arr[$condition['sub_dim_type']] = $data['list'];
            return $arr;
        }
    }
    /**
     * type为 frequency_one_dim 处理数组的方法 （多维度命中）
     * @param $condition
     * @return array
     */
    private function frequency_one_dim($condition)
    {
        $arr = [];
        unset($condition['result'],$condition['type'],$condition['score']);
        array_push($arr,$condition);
        return $arr;
    }
    /**
     * type为 black_list 处理数组的方法 （多维度命中）
     * @param $condition
     * @return array
     */
    private function black_list($condition)
    {
        $arr = [];
        $hits = $condition['hits'];
        foreach ($hits as $key => $value){
            if(array_key_exists('execute_court', $value)){
                # 把法院失信的单独提取出来
                array_push($this->execute_court, $value);
            }else{
                # 其他的存到一个数组
                array_push($this->fraud_type_display_name, $value['fraud_type_display_name']);
            }
        }
    }
    /**
     * type为 discredit_count 处理数组的方法 （多维度命中）
     * @param $condition
     * @return array
     */
    private function discredit_count($condition)
    {
        $arr = [];
        $hits = $condition['hits'];
        foreach ($hits as $key => $value){
            unset($value['value']);
            array_push($arr,$value);
        }
        return $arr;
    }
    /**
     * type为 cross_velocity_one_dimt 处理数组的方法 （单维度命中）
     * @param $condition
     * @return array
     */
    private function cross_velocity_one_dim($condition)
    {

    }

    /**
     * 定义一个{风险决策接口}数组的处理方法
     * @param $result
     * @return mixed
     */
    private function service_hit_rules($result)
    {
        # 定一个空数组作为 hit_rules 的容器
        $hit_rules = $result['hit_rules'];
        $hit_rule_arr = [];
        unset($result['policy_set'],$result['policy_set_name']);
        if($result['device_info']['code'] == !411){

        }else{
            unset($result['device_info']);
        }
        foreach ($hit_rules as $key => $value) {
            unset($value['id'], $value['uuid']);
            array_push($hit_rule_arr, $value);
        }
        $result['hit_rules'] = $hit_rule_arr;
        return $result;
    }

    /**
     * 对处理好的$cross_partner数组进行二次处理
     * 组装关联平台和次数组成的数组
     * @param $cross_partner
     * @return array
     */
    private function second_cross_partner($cross_partner)
    {
        # 定义一个空数组($arr) 用于存储一个二维数组，每个子数组存在key(平台名)=>value(次数count) 重复
        $arr = [];
        $industry_display_name = array_column($cross_partner,'industry_display_name');
        $count = array_column($cross_partner,'count');

        for($i=0;$i<count($cross_partner);$i++){
            array_push($arr,[$industry_display_name[$i] => $count[$i]]);
        }
        # 定义一个空数组($data) 用于存储统计所有平台以及对应的次数(不重复)
        $data = [];
        for($n=0;$n<count($arr);$n++){
            foreach ($arr[$n] as $key => $value){
                if(array_key_exists($key,$data) == true){
                    $data[$key] = $data[$key] + $value;
                }else{
                    $data[$key] = $value;
                }
            }
        }
        return $data;
    }
}